package com.jxwifi.cloud.quickclean.toolkit;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.idroid.widget.LoadDialog;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.utils.KAlertDialog;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TKDetailsActivity extends ClientBasicActivity implements TKDetailsView {

    @Bind(R.id.tv_tk_details_name)
    TextView mTvTkDetailsName;
    @Bind(R.id.tv_tk_details_phone)
    TextView mTvTkDetailsPhone;
    @Bind(R.id.tv_tk_details_address)
    TextView mTvTkDetailsAddress;
    @Bind(R.id.tv_tk_details_starts)
    TextView mTvTkDetailsStarts;
    @Bind(R.id.rv_tk_details_list)
    RecyclerView mRvTkDetailsList;
    @Bind(R.id.tv_tk_details_price)
    TextView mTvTkDetailsPrice;
    @Bind(R.id.tv_tk_details_order_number)
    TextView mTvTkDetailsOrderNumber;//订单编号
    @Bind(R.id.tv_tk_details_order_time)
    TextView mTvTkDetailsOrderTime;//创建时间
    @Bind(R.id.tv_tk_details_order_deliver_title)
    TextView mTvTkDetailsOrderDeliverTitle;
    @Bind(R.id.tv_tk_details_order_deliver)
    TextView mTvTkDetailsOrderDeliver;//发货时间
    @Bind(R.id.tv_tk_details_order_logistics)
    TextView mTvTkDetailsOrderLogistics;//物流单号
    @Bind(R.id.tv_tk_details_order_logistics_company)
    TextView mTvTkDetailsOrderLogisticsCompany;//物流公司
    @Bind(R.id.tv_tk_details_refund)
    TextView mTvTkDetailsRefund;
    @Bind(R.id.tv_tk_details_payment)
    TextView mTvTkDetailsPayment;
    @Bind(R.id.tv_tk_details_receiving)
    TextView mTvTkDetailsReceiving;
    @Bind(R.id.tv_tk_details_logistics)
    TextView mTvTkDetailsLogistics;
    @Bind(R.id.lin_tk_details_start)
    LinearLayout mLinTkDetailsStart;
    @Bind(R.id.tv_tk_details_order_logistics_title)
    TextView mTvTkDetailsOrderLogisticsTitle;
    @Bind(R.id.tv_tk_details_copy)
    TextView mTvTkDetaliCopy;
    @Bind(R.id.tv_tk_details_order_logistics_company_title)
    TextView mTvTkDetailsOrderLogisticsCompnyTitle;
    @Bind(R.id.sv_tk_order_details)
    ScrollView mSvTkOrderDetails;

    private TKDetailsPresenter mPresenter;
    private String TKOrder;
    private LoadDialog mLoadDialog;//加载动画

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_kit_details);
        ButterKnife.bind(this);
        openEventBus();
        TKOrder = getIntent().getStringExtra("tk_code");
        initHead();
        mPresenter = new TKDetailsPresenter(mBaseContext, this);
        mLoadDialog = new LoadDialog(mBaseContext, "");
        mLoadDialog.show();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("工具包详情");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    @OnClick({R.id.tv_tk_details_refund, R.id.tv_tk_details_payment, R.id.tv_tk_details_after_sale, R.id.tv_tk_details_receiving
            , R.id.tv_tk_details_copy, R.id.tv_tk_details_logistics})
    void clickCK(View v) {
        switch (v.getId()) {
            //取消订单
            case R.id.tv_tk_details_refund:
                final KAlertDialog dialogshow=new KAlertDialog(mBaseContext,"确认是否要取消订单？");
                dialogshow.setRightTxt("确定", new NoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {

                        mPresenter.OrderCancel();
                        dialogshow.dismiss();
                    }
                });
                dialogshow.show();
                MobclickAgent.onEvent(mBaseContext, "tv_tk_details_refund");
                break;
            //付款
            case R.id.tv_tk_details_payment:
                mPresenter.paySetMeal();
                MobclickAgent.onEvent(mBaseContext, "tv_tk_details_payment");
                break;
            //售后客服
            case R.id.tv_tk_details_after_sale:
                mPresenter.inittell();
                MobclickAgent.onEvent(mBaseContext, "tv_tk_details_after_sale");
                break;
            //收货
            case R.id.tv_tk_details_receiving:
                final KAlertDialog dialogshow1=new KAlertDialog(mBaseContext,"是否确认收货");
                dialogshow1.setRightTxt("确定", new NoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {

                        mPresenter.Receiving();
                        dialogshow1.dismiss();
                    }
                });
                dialogshow1.show();
                MobclickAgent.onEvent(mBaseContext, "tv_tk_details_receiving");
                break;
            //复制
            case R.id.tv_tk_details_copy:
                mPresenter.KtCopy();
                break;
            //查看物流
            case R.id.tv_tk_details_logistics:
                mPresenter.Logistics();
                MobclickAgent.onEvent(mBaseContext, "tv_tk_details_logistics");
                break;
        }
    }

    @Override
    public void oninitData() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.initData();
        }
    }

    @Override
    public ScrollView getSvTkOrderDetails() {
        return mSvTkOrderDetails;
    }

    @Override
    public String getTKOrder() {
        return TKOrder;
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public void onOrderCancel() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.OrderCancel();
        }
    }

    @Override
    public void oninittell() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.inittell();
        }
    }

    @Override
    public void onReceiving() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.Receiving();
        }
    }

    @Subscriber(tag = "e-PayResult")
    private void getCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        mPresenter.initData();
    }

    @Subscriber(tag = "e-WXPayResult")
    private void getWXCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        mPresenter.initData();
    }

    @Override
    public TextView getCompnyTitle() {
        return mTvTkDetailsOrderLogisticsCompnyTitle;
    }

    @Override
    public TextView getDetaliCopy() {
        return mTvTkDetaliCopy;
    }

    @Override
    public TextView getLogisticsTitle() {
        return mTvTkDetailsOrderLogisticsTitle;
    }

    @Override
    public TextView getDetailsRefund() {
        return mTvTkDetailsRefund;
    }

    @Override
    public TextView getDetailsPayment() {
        return mTvTkDetailsPayment;
    }

    @Override
    public TextView getDetailsReceiving() {
        return mTvTkDetailsReceiving;
    }

    @Override
    public TextView getDetailsLogistics() {
        return mTvTkDetailsLogistics;
    }

    @Override
    public LinearLayout getDetailsStart() {
        return mLinTkDetailsStart;
    }

    @Override
    public TextView getDeliverTitle() {
        return mTvTkDetailsOrderDeliverTitle;
    }

    @Override
    public TextView getOrderLogisticsCompany() {
        return mTvTkDetailsOrderLogisticsCompany;
    }

    @Override
    public TextView getOrderDeliver() {
        return mTvTkDetailsOrderDeliver;
    }

    @Override
    public TextView getOrderTime() {
        return mTvTkDetailsOrderTime;
    }

    @Override
    public TextView getOrderNumber() {
        return mTvTkDetailsOrderNumber;
    }

    @Override
    public TextView getOrderLogistics() {
        return mTvTkDetailsOrderLogistics;
    }

    @Override
    public TextView getTkDetailsPrice() {
        return mTvTkDetailsPrice;
    }

    @Override
    public RecyclerView getRvList() {
        return mRvTkDetailsList;
    }

    @Override
    public TextView getTkDetailsStarts() {
        return mTvTkDetailsStarts;
    }

    @Override
    public TextView getTkDetailsAddress() {
        return mTvTkDetailsAddress;
    }

    @Override
    public TextView getTkDetailsPhone() {
        return mTvTkDetailsPhone;
    }

    @Override
    public TextView getTkDetailsName() {
        return mTvTkDetailsName;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLoadDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter.initData();
                mLoadDialog.dismiss();
            }
        }, 1000);
    }
}
